//
//  TodayViewController.m
//  TotalTip
//
//  Created by gujian on 16/4/19.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>

@interface TodayViewController () <NCWidgetProviding>

@property (weak, nonatomic) IBOutlet UILabel *countTimeLbl;

@property (nonatomic, weak) NSTimer *timer;

@property (nonatomic, assign) NSTimeInterval totalTime;

@end

@implementation TodayViewController

/**
 *https://onevcat.com/2014/08/notification-today-widget/
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.totalDemoShareDefaults"];
    
    NSTimeInterval __unused leftInterval = [userDefaults integerForKey:@"goup.totalDemoShareDefaults.left"];
    _totalTime = [userDefaults integerForKey:@"group.totalDemoShareDefaults.total"];
    
    [self startCountDown];
}

- (void)startCountDown
{
    [self stopCountDown];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(update) userInfo:nil repeats:YES];
    self.timer = timer;
    
}

- (void)stopCountDown
{
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}


- (void)pauseCountDown
{
    [self stopCountDown];
    
    NSTimeInterval leftInterval = self.totalTime - [[NSDate date] timeIntervalSince1970];
    
    if (leftInterval <= 0) {
        leftInterval = 0;
    }
    
    self.totalTime = [[NSDate date] timeIntervalSince1970] + leftInterval;
}

- (void)update
{
    NSTimeInterval leftInterval = self.totalTime - [[NSDate date] timeIntervalSince1970];
    
    if (leftInterval <= 0) {
        leftInterval = 0;
        self.countTimeLbl.text = @"00:00:00:00";
        [self stopCountDown];
        [self addOpenBtn];
        return;
    }
    
    NSInteger totalSeconds = (NSInteger)leftInterval;
    NSInteger hour = totalSeconds/3600;
    NSInteger min = (totalSeconds/60)%60;
    NSInteger sec = totalSeconds%60;
    CGFloat msec = leftInterval - totalSeconds;
    
    NSMutableString *timeStr = [[NSMutableString alloc] init];
    
    if (hour) {
        [timeStr appendFormat:@"%02d:",(int)hour];
    }
    
    [timeStr appendFormat:@"%02d:",(int)min];
    [timeStr appendFormat:@"%02d:",(int)sec];
    [timeStr appendFormat:@"%02d",(int)(msec * 1000)];
    
    self.countTimeLbl.text = timeStr;

    
}

- (void)addOpenBtn
{
    self.preferredContentSize = CGSizeMake(0, 100);
    UIButton *openBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 50, 30)];
    [openBtn setTitle:@"打开" forState:UIControlStateNormal];
    [openBtn addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:openBtn];
}

- (void)openAction:(id)sender
{
    [self.extensionContext openURL:[NSURL URLWithString:@"TotalDemo://finished"] completionHandler:^(BOOL success) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
    // Perform any setup necessary in order to update the view.
    
    // If an error is encountered, use NCUpdateResultFailed
    // If there's no update required, use NCUpdateResultNoData
    // If there's an update, use NCUpdateResultNewData

    completionHandler(NCUpdateResultNewData);
}

@end
